package com.youdianda.service.impl;

import com.youdianda.constant.ConfigFgxConstant;
import com.youdianda.dto.SysConfigFgxDTO;
import com.youdianda.entity.SysConfig;
import com.youdianda.entity.SysFgxConfig;
import com.youdianda.mapper.SysConfigFgxMapper;
import com.youdianda.mapper.SysIndexFgxMapper;
import com.youdianda.result.Result;
import com.youdianda.service.SysConfigFgxService;
import com.youdianda.vo.ConfigFgxVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class SysConfigFgxServiceImpl implements SysConfigFgxService {

    @Autowired
    private SysConfigFgxMapper sysConfigMapper;

    @Autowired
    private SysIndexFgxMapper indexMapper;

    /**
     * 所有配置数据
     * allData
     * list  配置数据
     * configForm
     * configRules
     *
     * @return
     */
    @Transactional
    @Override
    public Map all() {

        Map map = new HashMap();
        List<SysConfig> allData = sysConfigMapper.allConfigList();
        map.put("allData",allData);
        Map configFormMap = new HashMap();
        allData.forEach(sysConfig -> {
            configFormMap.put(sysConfig.getAttr_key(),sysConfig.getAttr_value());
        });
        map.put("configForm",configFormMap);
        Map configRulesMap = new HashMap();
        configRulesMap.put("site_name", configFormMap.get("site_name"));
        map.put("configRules",configRulesMap);

        List<SysConfig> systemConfig = indexMapper.getConfig(ConfigFgxConstant.SYSTEM);
        List<SysConfig> baseConfig = indexMapper.getConfig(ConfigFgxConstant.BASE);
        List<SysConfig> customConfig = indexMapper.getConfig(ConfigFgxConstant.CUSTOM);

        List configList = new ArrayList();
        Map baseMap = new HashMap();
        Map customMap = new HashMap();
        Map systemMap = new HashMap();

        List baseList = new ArrayList();
        List customList = new ArrayList();
        List systemList = new ArrayList();

        if (baseConfig != null && baseConfig.size() > 0){
            //取出名字去重
            Set<String> set = new TreeSet();
            baseConfig.forEach(sysConfig -> {
                set.add(sysConfig.getGroup_value());
            });

            //倒叙输出
            Set<String> set2 = new TreeSet<String>(Comparator.reverseOrder());
            set2.addAll(set);

            set2.forEach(s -> {
                List<ConfigFgxVO> voList = indexMapper.checkByGroupValue(s);
                Map baseMapData = new HashMap();
                baseMapData.put("name",s);
                baseMapData.put("data",voList);
                baseList.add(baseMapData);
            });
        }
        baseMap.put("name","基础设置");
        baseMap.put("key",ConfigFgxConstant.BASE);
        baseMap.put("children",baseList);

        if (customConfig != null && customConfig.size() > 0){
            //取出名字去重
            Set<String> set = new TreeSet();
            customConfig.forEach(sysConfig -> {
                set.add(sysConfig.getGroup_value());
            });

            set.forEach(s -> {
                List<ConfigFgxVO> voList = indexMapper.checkByGroupValue(s);
                Map customMapData = new HashMap();
                customMapData.put("name",s);
                customMapData.put("data",voList);
                customList.add(customMapData);
            });
        }
        customMap.put("name","其他设置");
        customMap.put("key",ConfigFgxConstant.CUSTOM);
        customMap.put("children",customList);

        if (systemConfig != null && systemConfig.size() > 0){
            //取出名字去重
            Set<String> set = new TreeSet();
            systemConfig.forEach(sysConfig -> {
                set.add(sysConfig.getGroup_value());
            });

            set.forEach(s -> {
                List<ConfigFgxVO> voList = indexMapper.checkByGroupValue(s);
                Map systemMapData = new HashMap();
                systemMapData.put("name",s);
                systemMapData.put("data",voList);
                systemList.add(systemMapData);
            });
        }
        systemMap.put("name","系统设置");
        systemMap.put("key",ConfigFgxConstant.SYSTEM);
        systemMap.put("children",systemList);

        configList.add(baseMap);
        configList.add(customMap);
        configList.add(systemMap);

        map.put("list",configList);
        return map;
    }

    @Transactional
    @Override
    public void update(List<SysConfigFgxDTO> dtos) {
        if (dtos != null && dtos.size() > 0){
            dtos.forEach(sysConfigFgxDTO -> {
                sysConfigMapper.update(sysConfigFgxDTO);
            });
        }
    }
}
